<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="@/assets/logo.png" alt="">
        <span>个人运动平台</span>
      </div>
      <el-button type="info" @click="logout">安全退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse?'64px':'200px'">
        <div class="toggle-button" @click="toggleCollapase">|||</div>
        <el-menu background-color="#545c64" text-color="#fff" active-text-color="#409eff" :collapse="isCollapse" :collapse-transition="false" :router="true" :default-active="activePath">
          <!-- 一级菜单 -->
          <el-submenu :index="item.path" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="iconsObject[item.id]"></i>
              <span>{{item.title}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="item.path" v-for="item in item.sList" :key="item.id" @click="saveNavState(item.path)">
              <template slot="title">
                <i :class="iconsObject[item.id]"></i>
                <span>{{item.title}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'home',
  data() {
    return {
      // 菜单列表
      menuList: [],
      isCollapse: false,
      iconsObject: {
        100: 'iconfont icon-jurassic_admin',
        200: 'iconfont icon-paobu',
        101: 'iconfont icon-yonghu',
        102: 'iconfont icon-mima',
        103: 'iconfont icon-paobu',
        104: 'iconfont icon-shangpin',
        201: 'iconfont icon-shu',
        202: 'iconfont icon-remen',
        203: 'iconfont icon-shiwu',
        204: 'iconfont icon-shiwu',
      },
      activePath: '/welcome',
    }
  },
  mounted() {},
  created() {
    // 查询menuList
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 安全退出
    logout() {
      window.sessionStorage.clear() //清除session
      this.$router.push('/login') //回到首页
    },
    // 获取导航菜单方法
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.flag == 200) {
        console.log('获取了导航菜单')
        console.log(this.menuList)
        this.menuList = res.menus
      } else {
        this.$message.error('获取列表失败!')
      }
    },
    //伸缩菜单栏
    toggleCollapase() {
      this.isCollapse = !this.isCollapse
    },
    //保存路径
    saveNavState(path) {
      window.sessionStorage.setItem('activePath', path)
      this.activePath = path
    },
  },
}
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  padding-left: 0%;
  align-items: center;
  background-color: #373d41;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
img {
  width: 55px;
  height: 55px;
}
// |||按钮样式
.toggle-button {
  background: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>